package com.example.gcmtest1;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.HttpVersion;

import com.example.gcmtest1.httpserver.HTTPProxyService;
import com.example.gcmtest1.youtube.DownloadAdapter;
import com.example.gcmtest1.youtube.SmartHTTPDownload;
import com.example.gcmtest1.youtube.YoutubeSQLRecord;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SimpleAdapter;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;

public class FileListActivity extends FragmentActivity implements OnItemClickListener {

	private String logName = "FileListActivity";
	
	private ArrayList<HashMap<String, String>> list =  new ArrayList<HashMap<String, String>>();
	private SimpleAdapter ad;
	
	private DownloadAdapter da;
	
	public FileListActivity() {
		Log.d(logName, "Constructor is created()");
		HTTPProxyService service = HTTPProxyService.getInstance();
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_list);
        
        View lv1 = (View) this.getWindow().getDecorView();
        
        if (findViewById(R.id.viewPager) == null) {
        	Log.d(logName, "Activity, viewPager is null");
        }
        
        ListView lv = (ListView) lv1.findViewById(R.id.fileListView);
        lv.setOnItemClickListener(this);
        
        if (lv1.findViewById(R.id.viewPager) == null) {
        	Log.d(logName, "View, viewPager is null");
        }
        
        this.ad = new SimpleAdapter(this, this.list, android.R.layout.simple_list_item_2, 
        		new String[] {"text1", "text2"}, new int[] {android.R.id.text1, android.R.id.text2});
        
        
        //a.add("A");
        //a.add("B");
        
        this.da = new DownloadAdapter(this, HTTPProxyService.fileListItems);
        //lv.setAdapter(ad);
        lv.setAdapter(da);
        
        //this.testAddItem();
        this.addItems();
        
        //a.add("C");
        
    }
    
    private void addItems() {
    	if (HTTPProxyService.getInstance().arrDownloading != null) {
    		for (SmartHTTPDownload dl : HTTPProxyService.getInstance().arrDownloading) {
    			HTTPProxyService.fileListItems.add(dl);
        	}	
    	}
    	
    }
    
    private void __addItems() {
    	YoutubeSQLRecord[] rr = HTTPProxyService.getInstance().sqlHelper.getAllRecords();
    	if (rr != null) {
    		for (YoutubeSQLRecord r : rr) {
    			HTTPProxyService.fileListItems.add(r);
    		}
    	}
    }
    
    private void _addItems() {
    	    	
    	File f = new File(YoutubeActivity.VIDEO_FOLDER);
    	File[] files = f.listFiles();
    	for (File ff : files) {
    		if (ff.isFile() == true) {
    			HashMap<String, String> h = new HashMap<String, String>();
    	    	h.put("text1", ff.getName());
    	    	
    	    	//long fileSize = ff.length() / (1024*1024);    	
    	    	//h.put("text2", String.valueOf(fileSize) + " M");
    	    	
    	    	long fileSize = ff.length();    	    	
    	    	h.put("text2", String.valueOf(fileSize) + " bytes");
    	    	
    	    	list.add(h);
    	    	if (HTTPProxyService.fileListItems.contains(ff) == false) {
    	    		HTTPProxyService.fileListItems.add(ff);
    	    		Log.d(logName, "added item");
    	    	}
    	    	
    	    	
    	    	
    		}
    	}
    }
    
    private void testAddItem() {
    	HashMap<String, String> h = new HashMap<String, String>();
    	h.put("text1", "Test11");
    	h.put("text2", "Test12");
    	list.add(h);
    	
    	this.ad.notifyDataSetChanged();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_file_list, menu);
        return true;
    }

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		HTTPProxyService.fileListItems.remove(arg2);
		this.da.notifyDataSetChanged();	
	}

    
}
